<?php
class Cms_Pager {
	// 每页记录数
	public $pageSize = 10;
	
	/**
	 * 总记录数
	 */
	public $totalCount = 0;
	
	/**
	 * 共多少页
	 *
	 * @var int
	 */
	public $pageCount = 0;
	
	/**
	 * 当前页的页码
	 */
	public $currentPage = 1;
	
	// 数据库类
	public $modelDb;
	
	// 分页名称，区分多个分页
	public $pageName;
	
	// 分页模板文件
	private $tplName;
	
	/**
	 * 分页模板输出的字符串
	 * 以/为基础，或?为基础
	 */
	private $pagePathType;
	
	/**
	 * 分页与当前URL的链接方式 / or ? or &
	 */
	private $pageSeparator;
	
	/**
	 * 参数赋值分割符号: / or =
	 *
	 * @var str
	 */
	private $paraUrlSp;
	
	/**
	 * 当前页面URL
	 *
	 * @var unknown_type
	 */
	private $pagerLocation;
	
	/**
	 * ajax方法名称
	 */
	private $ajaxfun;
	
	/**
	 * 是否自动构造ajax方法
	 * 基于jQuery实现
	 *
	 * @var bool
	 */
	private $ajaxfunAutoBuild;
	
	private $ajaxfunUrl;
	
	private $staticLocation = null;
	
	/**
	 *
	 * @return string
	 */
	public function getStaticLocation() {
		return $this->staticLocation;
	}
	
	/**
	 *
	 * @param $staticLocation string       	
	 */
	public function setStaticLocation($staticLocation) {
		$this->staticLocation = $staticLocation;
	}
	
	/**
	 * 当前页面请求的参数
	 */
	// public $pagerRequest;
	// public $pagerRequest_arr;
	
	/**
	 * 构造函数
	 *
	 * @param $model_db class
	 *       	 的数据库类
	 * @param $page_name str
	 *       	 分页名称 $_GET['pagename'],默认为 page
	 * @param $tpl_name str
	 *       	 分页显示的模板名称
	 * @param $page_pathtype str
	 *       	 分页链接格式 ? , /+
	 */
	function __construct($modelDb = null, $page_name = "page", $tpl_name = 'default') {
		$this->modelDb = $modelDb;
		$page_name ? $this->pageName = $page_name : $this->pageName = "page";
		$tpl_name ? $this->tplName = $tpl_name : $this->tplName = "default";
		
		// URL规则适配
		$this->pagePathType = "?";
		$this->paraUrlSp = "=";
		
		// $page_pathtype!="?" ? $this->pagePathType = "/" :
		// $page_pathtype!="?" ? $this->paraUrlSp = "/" :
		
		// 初始化当前分页的URL
		// $this->initpgUrl($this->pageSeparator);
	}
	
	function setAjaxFun($funname, $url = null, $auto_buildFun = false) {
		$this->ajaxfun = $funname;
		$this->ajaxfunUrl = $url;
		$this->ajaxfunAutoBuild = $auto_buildFun;
	}
	
	function bulidAjaxFunName($page_num) {
		if ($this->ajaxfunUrl) {
			return $this->ajaxfun . "('" . $this->ajaxfunUrl . "','" . $page_num . "')";
		} else {
			return $this->ajaxfun . "(" . $page_num . ")";
		}
	}
	
	/**
	 * 得到 limit
	 *
	 * @param $_page_size;每页多少条 int       	
	 * @param $sql_conditions;总数 string
	 *       	 数据库查询条件
	 * @param $total;总数 int       	
	 * @param $para_arr;附加参数 array       	
	 * @return array
	 */
	function loadPagerData($_page_size = null, $sql_conditions = null, $total = null, $para_arr = null) {
		$this->initPagerLocation ( $para_arr );
		if ($_page_size && is_numeric ( $_page_size )) {
			$this->pageSize = $_page_size;
		}
		if ($this->modelDb) {
			$this->totalCount = $this->modelDb->loadCount ( null, $sql_conditions );
		} elseif ($total && is_numeric ( $total )) {
			$this->totalCount = $total;
		} else {
			$this->totalCount = 0;
		}
		
		// 总页数
		if ($this->totalCount) {
			$this->pageCount = ceil ( $this->totalCount / $this->pageSize );
		}
		// 当总数大于0
		return $this->loadPagerLimit ( $this->pageSize, $this->pageName );
	}
	
	function loadPagerLimit($_page_size = null, $_page_name = null) {
		if (! isset ( $_GET [$_page_name] ) || ! $_GET [$_page_name] || ! isset ( $_POST [$_page_name] ) || ! $_POST [$_page_name]) {
			$_page_name = $this->pageName;
		}
		$page_size = $_page_size && is_numeric ( $_page_size ) ? $_page_size : $this->pageSize;
		if (isset ( $_GET [$_page_name] ) && $_GET [$_page_name] && is_numeric ( $_GET [$_page_name] )) {
			$this->currentPage = $_GET [$_page_name];
		} else if (isset ( $_POST [$_page_name] ) && $_POST [$_page_name] && is_numeric ( $_POST [$_page_name] )) {
		} else {
			$this->currentPage = 1;
		}
		// dump($this->currentPage);exit();
		$count = $page_size;
		$offset = ($this->currentPage - 1) * $page_size;
		
		return array ($count, $offset );
	}
	
	/**
	 * 初始化当前页面的URL
	 */
	function initPagerLocation($para_arr = null) {
		$_pagerLocation = "http://" . $_SERVER ['HTTP_HOST'] . $_SERVER ['REQUEST_URI']; // 当前网页的基本路径
		
		$this->pagerLocation = preg_replace ( "((\&|\?)" . $this->pageName . "=(\d+))is", "", $_pagerLocation );
		$_pager_sp = explode ( "?", $this->pagerLocation );
		$this->pagePathType == '?' && isset ( $_pager_sp [1] ) && $_pager_sp [1] ? $this->pageSeparator = '&' : $this->pageSeparator = '?';
		// if( $this->pagePathType=='?' ) {
		if ($para_arr) {
			foreach ( $para_arr as $request ) {
				$_request = explode ( '=', $request );
				$_para_arr [$_request [0]] = $_request [1];
			}
			$_para_arr [$this->pageName] = 1;
			
			$this->pagerLocation = preg_replace ( "((\&|\?)" . $this->pageName . "=(\d+))is", "", $this->pagerLocation );
			$this->pageSeparator = '&';
		}
	}
	
	/**
	 * 调用分页模板
	 *
	 * @return HTML
	 */
	function loadPagerTpl() {
		if ($this->totalCount > 0) {
			
			// 如果强行设置location 那么使用设置的location
			if ($this->staticLocation) {
				$this->pagerLocation = $this->staticLocation;
				$this->pageSeparator = '&';
			}
			
			$pagertpl = "PageTpl_" . $this->tplName;
			return $this->$pagertpl ();
		}
		return null;
	}
	
	/**
	 * 简单分页方式
	 *
	 * @return string
	 */
	function PageTpl_Simple() {
		if ($this->currentPage != 1) {
			$First = "<a href=\"" . $this->pagerLocation . $this->pageSeparator . $this->pageName . $this->paraUrlSp . "1\" class=\"a1\">首页</a> ";
			$PrePage = "<a href=\"" . $this->pagerLocation . $this->pageSeparator . $this->pageName . $this->paraUrlSp . ($this->currentPage - 1) . "\" class=\"a1\">上一页</a> ";
		}
		
		// 只显示最近的5页
		($this->currentPage - 2) >= 2 ? $StartPage = $this->currentPage - 2 : $StartPage = $this->currentPage;
		($this->pageCount - $this->currentPage) >= 2 ? $LastPage = $this->currentPage + 2 : $LastPage = $this->pageCount;
		
		if ($this->currentPage <= 3) {
			$StartPage = 1;
			$LastPage = 5;
		}
		if ($this->pageCount - $this->currentPage < 3) {
			$StartPage = $this->pageCount - 4;
			$LastPage = $this->pageCount;
		}
		if ($this->pageCount <= 5) {
			$StartPage = 1;
			$LastPage = $this->pageCount;
		}
		
		while ( $StartPage <= $LastPage ) {
			if ($StartPage == $this->currentPage) {
				$ShowPage = " <span>" . $StartPage . "</span> ";
			} else {
				$ShowPage = "<a href=\"" . $this->pagerLocation . $this->pageSeparator . $this->pageName . $this->paraUrlSp . $StartPage . "\">" . $StartPage . "</a> ";
			}
			$page_nums .= "$ShowPage";
			$StartPage ++;
		}
		if ($this->currentPage != $this->pageCount) {
			$next_page .= " <a href=\"" . $this->pagerLocation . $this->pageSeparator . $this->pageName . $this->paraUrlSp . ($this->currentPage + 1) . "\" class=\"a1\">下一页</a> ";
			$last_page .= " <a href=\"" . $this->pagerLocation . $this->pageSeparator . $this->pageName . $this->paraUrlSp . $this->pageCount . "\" class=\"a1\">最后</a> ";
		}
		return $First . $PrePage . $page_nums . $next_page . $last_page . " 共 " . $this->pageCount . " 页 " . $this->totalCount . " 条";
	}
	
	/**
	 * Ajax分页
	 *
	 * @return string
	 */
	function PageTpl_Ajax() {
		$page_middle = 5;
		$page_nums = null;
		$PrePage = null;
		$nextPage = null;
		
		if ($this->currentPage != 1) {
			$PrePage = "<a href=\"javascript:" . $this->bulidAjaxFunName ( $this->currentPage - 1 ) . ";\" class=\"a1\">上一页</a> ";
		}
		if ($this->pageCount > $page_middle + 8) {
			$PrePage .= "<a href=\"javascript:void(0)\" onclick=\"" . $this->bulidAjaxFunName ( 1 ) . ";\">1</a> ";
			$PrePage .= "<a href=\"javascript:void(0)\" onclick=\"" . $this->bulidAjaxFunName ( 2 ) . ";\">1</a> ";
			$PrePage .= "... ";
			$nextPage .= "... ";
			$nextPage .= "<a href=\"javascript:void(0)\" onclick=\"" . $this->bulidAjaxFunName ( $this->pageCount - 1 ) . ";\">" . $this->pageCount . "</a> ";
			$nextPage .= "<a href=\"javascript:void(0)\" onclick=\"" . $this->bulidAjaxFunName ( $this->pageCount ) . ";\">" . $this->pageCount . "</a> ";
			
			if ($this->currentPage <= $page_middle) {
				$PrePage = null;
				$StartPage = 1;
				$LastPage = $page_middle + 2;
			} else {
				$StartPage = $this->currentPage - (($page_middle - 1) / 2);
				if ($this->currentPage < $this->pageCount - $page_middle) {
					$LastPage = $StartPage + $page_middle - 1;
				} else {
					$LastPage = $this->pageCount;
					$nextPage = null;
				}
			}
		} else {
			$StartPage = 1;
			$LastPage = $this->pageCount;
		}
		
		while ( $StartPage <= $LastPage ) {
			if ($StartPage == $this->currentPage) {
				$ShowPage = "<span>" . $StartPage . "</span> ";
			} else {
				$ShowPage = "<a href=\"javascript:void(0)\" onclick=\"" . $this->bulidAjaxFunName ( $StartPage ) . ";\">" . $StartPage . "</a> ";
			}
			$page_nums .= $ShowPage;
			$StartPage ++;
		}
		
		if ($this->currentPage != $this->pageCount) {
			$nextPage .= "<a href=\"javascript:" . $this->bulidAjaxFunName ( $this->currentPage + 1 ) . ";\" class=\"a1\">下一页</a> ";
		}
		// return $PrePage . $page_nums . $nextPage . "<p>(共 " .
		// $this->pageCount . " 页 " . $this->totalCount . " 条)</p>";
		return $PrePage . $page_nums . $nextPage;
	}
	
	/**
	 * 显示更多分页方式
	 */
	function PageTpl_Boxmore() {
		$page_middle = 5;
		$page_nums = null;
		$PrePage = null;
		$nextPage = null;
		if ($this->pageCount > $page_middle + 8) {
			$PrePage .= "<a href=\"" . $this->pagerLocation . $this->pageSeparator . $this->pageName . $this->paraUrlSp . "1\">1</a> ";
			$PrePage .= "<a href=\"" . $this->pagerLocation . $this->pageSeparator . $this->pageName . $this->paraUrlSp . "2\">2</a> ";
			$PrePage .= "... ";
			$nextPage .= "... ";
			$nextPage .= "<a href=\"" . $this->pagerLocation . $this->pageSeparator . $this->pageName . $this->paraUrlSp . ($this->pageCount - 1) . "\">" . ($this->pageCount - 1) . "</a> ";
			$nextPage .= "<a href=\"" . $this->pagerLocation . $this->pageSeparator . $this->pageName . $this->paraUrlSp . $this->pageCount . "\">" . $this->pageCount . "</a> ";
			
			if ($this->currentPage <= $page_middle) {
				$PrePage = null;
				$StartPage = 1;
				$LastPage = $page_middle + 2;
			} else {
				$StartPage = $this->currentPage - (($page_middle - 1) / 2);
				if ($this->currentPage < $this->pageCount - $page_middle) {
					$LastPage = $StartPage + $page_middle - 1;
				} else {
					$LastPage = $this->pageCount;
					$nextPage = null;
				}
			}
			
			while ( $StartPage <= $LastPage ) {
				if ($StartPage == $this->currentPage) {
					$ShowPage = "<span>" . $StartPage . "</span> ";
				} else {
					$ShowPage = "<a href=\"" . $this->pagerLocation . $this->pageSeparator . $this->pageName . $this->paraUrlSp . $StartPage . "\">" . $StartPage . "</a> ";
				}
				$page_nums .= $ShowPage;
				$StartPage ++;
			}
		
		} else {
			$StartPage = 1;
			while ( $StartPage <= $this->pageCount ) {
				if ($StartPage == $this->currentPage) {
					$ShowPage = "<span>" . $StartPage . "</span> ";
				} else {
					$ShowPage = "<a href=\"" . $this->pagerLocation . $this->pageSeparator . $this->pageName . $this->paraUrlSp . $StartPage . "\">" . $StartPage . "</a> ";
				}
				$page_nums .= $ShowPage;
				$StartPage ++;
			}
		
		}
		return $PrePage . $page_nums . $nextPage;
	}
	
	/**
	 * 默认分页
	 *
	 * @return string
	 */
	function PageTpl_Default() {
		$page_middle = 5;
		$page_nums = null;
		$PrePage = null;
		$nextPage = null;
		
		if ($this->currentPage != 1) {
			$PrePage = "<a href=\"" . $this->pagerLocation . $this->pageSeparator . $this->pageName . $this->paraUrlSp . ($this->currentPage - 1) . "\" class=\"a1\">上一页</a> ";
		}
		if ($this->pageCount > $page_middle + 8) {
			$PrePage .= "<a href=\"" . $this->pagerLocation . $this->pageSeparator . $this->pageName . $this->paraUrlSp . "1\">1</a> ";
			$PrePage .= "<a href=\"" . $this->pagerLocation . $this->pageSeparator . $this->pageName . $this->paraUrlSp . "2\">2</a> ";
			$PrePage .= "... ";
			$nextPage .= "... ";
			$nextPage .= "<a href=\"" . $this->pagerLocation . $this->pageSeparator . $this->pageName . $this->paraUrlSp . ($this->pageCount - 1) . "\">" . ($this->pageCount - 1) . "</a> ";
			$nextPage .= "<a href=\"" . $this->pagerLocation . $this->pageSeparator . $this->pageName . $this->paraUrlSp . $this->pageCount . "\">" . $this->pageCount . "</a> ";
			
			if ($this->currentPage <= $page_middle) {
				$PrePage = null;
				$StartPage = 1;
				$LastPage = $page_middle + 2;
			} else {
				$StartPage = $this->currentPage - (($page_middle - 1) / 2);
				if ($this->currentPage < $this->pageCount - $page_middle) {
					$LastPage = $StartPage + $page_middle - 1;
				} else {
					$LastPage = $this->pageCount;
					$nextPage = null;
				}
			}
		} else {
			$StartPage = 1;
			$LastPage = $this->pageCount;
		}
		
		while ( $StartPage <= $LastPage ) {
			if ($StartPage == $this->currentPage) {
				$ShowPage = "<span>" . $StartPage . "</span> ";
			} else {
				$ShowPage = "<a href=\"" . $this->pagerLocation . $this->pageSeparator . $this->pageName . $this->paraUrlSp . $StartPage . "\">" . $StartPage . "</a> ";
			}
			$page_nums .= $ShowPage;
			$StartPage ++;
		}
		
		if ($this->currentPage != $this->pageCount) {
			$nextPage .= "<a href=\"" . $this->pagerLocation . $this->pageSeparator . $this->pageName . $this->paraUrlSp . ($this->currentPage + 1) . "\" class=\"a1\">下一页</a> ";
		}
		// return $PrePage . $page_nums . $nextPage . "<p>(共 " .
		// $this->pageCount . " 页 " . $this->totalCount . " 条)</p>";
		return $PrePage . $page_nums . $nextPage;
	}
}
?>